package com.ywjt.platformbusiness.store.adapter;

import android.graphics.Color;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ywjt.platformbusiness.R;
import com.ywjt.platformbusiness.order.adapter.OrderListChild_Adapter;
import com.ywjt.platformbusiness.store.order.model.StoreOrderListModel;
import java.util.List;

import me.samlss.broccoli.Broccoli;
import me.samlss.broccoli.BroccoliGradientDrawable;
import me.samlss.broccoli.PlaceholderParameter;

public class StoreOrderListAdapter extends BaseQuickAdapter<StoreOrderListModel.ResultDTO.RecordsDTO, BaseViewHolder> {
    private int status=0;
    public StoreOrderListAdapter(@Nullable List<StoreOrderListModel.ResultDTO.RecordsDTO> data, int status) {
        super(R.layout.item_store_order_list, data);
        this.status=status;
    }

    @Override
    protected void convert(BaseViewHolder helper, StoreOrderListModel.ResultDTO.RecordsDTO item) {
        RelativeLayout llunpay=helper.getView(R.id.llunpay);
        RelativeLayout llunconfirm=helper.getView(R.id.llunconfirm);
        RelativeLayout llunsend=helper.getView(R.id.llunsend);
        RelativeLayout llReturn=helper.getView(R.id.llReturn);
        TextView tvStatus=helper.getView(R.id.tvStatus);


        Broccoli broccoli = null;
        if (broccoli == null){
            broccoli = new Broccoli();
        }

        broccoli.removeAllPlaceholders();
        broccoli.addPlaceholder(new PlaceholderParameter.Builder()
                .setView(helper.getView(R.id.tvOrdernum))
                .setDrawable(new BroccoliGradientDrawable(Color.parseColor("#DDDDDD"),
                        Color.parseColor("#CCCCCC"), 0, 1000, new LinearInterpolator()))
                .build());
        broccoli.addPlaceholder(new PlaceholderParameter.Builder()
                .setView(helper.getView(R.id.tvTotalMoney))
                .setDrawable(new BroccoliGradientDrawable(Color.parseColor("#DDDDDD"),
                        Color.parseColor("#CCCCCC"), 0, 1000, new LinearInterpolator()))
                .build());
        broccoli.addPlaceholder(new PlaceholderParameter.Builder()
                .setView(helper.getView(R.id.tvPay))
                .setDrawable(new BroccoliGradientDrawable(Color.parseColor("#DDDDDD"),
                        Color.parseColor("#CCCCCC"), 0, 1000, new LinearInterpolator()))
                .build());
        broccoli.addPlaceholder(new PlaceholderParameter.Builder()
                .setView(helper.getView(R.id.tvPersonInfo))
                .setDrawable(new BroccoliGradientDrawable(Color.parseColor("#DDDDDD"),
                        Color.parseColor("#CCCCCC"), 0, 1000, new LinearInterpolator()))
                .build());
        broccoli.addPlaceholder(new PlaceholderParameter.Builder()
                .setView(helper.getView(R.id.tvPersonAddress))
                .setDrawable(new BroccoliGradientDrawable(Color.parseColor("#DDDDDD"),
                        Color.parseColor("#CCCCCC"), 0, 1000, new LinearInterpolator()))
                .build());
        broccoli.addPlaceholder(new PlaceholderParameter.Builder()
                .setView(helper.getView(R.id.tvStatus))
                .setDrawable(new BroccoliGradientDrawable(Color.parseColor("#DDDDDD"),
                        Color.parseColor("#CCCCCC"), 0, 1000, new LinearInterpolator()))
                .build());
        broccoli.show();


        Runnable task = null;
        if (task == null){
            final Broccoli finalBroccoli = broccoli;
            task = new Runnable() {
                @Override
                public void run() {
                    //when you need to update data, you must to call the remove or the clear method.
                    finalBroccoli.removeAllPlaceholders();

                    if (item==null){
                        return;
                    }
                    helper.setText(R.id.tvOrdernum,"订单号："+item.getOrderNumber());
                    helper.setText(R.id.tvTotalMoney,item.getActuallyMoney()+"");
                    helper.setText(R.id.tvPay,"共"+item.getGoodsNum()+"件商品  实付：");
                    helper.setText(R.id.tvPersonInfo,item.getReceiveName()+" "+item.getReceivePhone());
                    helper.setText(R.id.tvPersonAddress,item.getReceiveAddress());

                    switch (item.getStatus()){
                        case 0:
                            tvStatus.setText("待付款");
                            llunpay.setVisibility(View.VISIBLE);
                            llunconfirm.setVisibility(View.GONE);
                            llunsend.setVisibility(View.GONE);
                            llReturn.setVisibility(View.GONE);
                            break;
                        case 1:
                            tvStatus.setText("待发货");
                            llunpay.setVisibility(View.GONE);
                            llunconfirm.setVisibility(View.GONE);
                            llunsend.setVisibility(View.VISIBLE);
                            llReturn.setVisibility(View.GONE);
                            break;
                        case 2:
                            tvStatus.setText("待收货");
                            llunpay.setVisibility(View.GONE);
                            llunsend.setVisibility(View.GONE);
                            llunconfirm.setVisibility(View.VISIBLE);
                            llReturn.setVisibility(View.GONE);
                            break;
                        case 3:
                            tvStatus.setText("已完成");
                            llunpay.setVisibility(View.GONE);
                            llunsend.setVisibility(View.GONE);
                            llunconfirm.setVisibility(View.GONE);
                            llReturn.setVisibility(View.GONE);
                            break;
                        case -2:
                            tvStatus.setText("售后");
                            llunpay.setVisibility(View.GONE);
                            llunsend.setVisibility(View.GONE);
                            llunconfirm.setVisibility(View.GONE);
                            llReturn.setVisibility(View.VISIBLE);
                            break;
                    }

                    helper.addOnClickListener(R.id.tvPriceModify);
                    helper.addOnClickListener(R.id.tvCheckTrace);
                    helper.addOnClickListener(R.id.tvTipSendGoods);
                    helper.addOnClickListener(R.id.tvCheckTraceReturn);


                    RecyclerView rcChildOrder=helper.getView(R.id.rcChildOrder);
                    OrderListChild_Adapter adapter=new OrderListChild_Adapter(item.getOrderDetailList(),1);
                    rcChildOrder.setLayoutManager(new LinearLayoutManager(mContext));
                    rcChildOrder.setAdapter(adapter);
                }
            };
        }else{
            helper.itemView.removeCallbacks(task);
        }
        helper.itemView.postDelayed(task, 1000);
    }
}
